<template>
	<view>
		<view class="addressList_box">
			<view class="addressList_con">
				<view class="addressItem" v-for="(item,index) in addressList" :key="index">
					
					<view class="consigneeCon" @click="setDefault(item.id)">
						<view class="consigneeName">{{item.name}}</view>
						<view class="consigneeTel">{{item.mobile}}</view>
					</view>
					<view class="addressDetail" @click="setDefault(item.id)">
						{{item.address}}
					</view>
					
					
					<view class="addressPost">
						<view class="addressCheck" @click="setDefault(item.id)">
							<icon type="success" color="#327FE8" class="totalSelect" v-if="item.is_default==1" />
							<icon type="circle" class="totalSelect" v-else />
							<text>设为默认地址</text>
						</view>
						<view class="addressHandle">
							<view class="addressDel" @click="delClick(item.id)">
								<u-icon name="trash" color="#f00" size="38"></u-icon>
							</view>
							
						</view>
					</view>
				</view>
			</view>
			<view class="dataEmpty_con" v-if="addressList.length == 0">
				<u-empty></u-empty>
			</view>
		</view>

		<!-- 底部操作 -->
		<view style="height: 90rpx"></view>
		<view class="formPost_con">
			<view class="submitBtn" @click="goPage('./addressAdd')">新增地址</view>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 下单页面参数confirm
				type: '',
				
				// 地址列表
				addressList: [],
			}
		},
		onLoad(option) {
			uni.showLoading({
				title: '加载中'
			});
			// 修改地址
			if (option.type) {
				this.type = option.type;
			}
		},
		onShow() {
			var _this = this;
			_this.getAddressList();
		},
		onReady() {

		},
		methods: {
			// 获取地址列表
			getAddressList() {
				var _this = this;
				_this.$u.get('/api/user.address/getList', {

				}).then(res => {
					console.log("==获取地址列表==");
					console.log(res);
					uni.hideLoading();
					_this.addressList = res.data;
				}).catch(res => {
					console.log(res);
				})
			},

			// 设为默认地址
			setDefault(address_id) {
				var _this = this;
				_this.$u.post('/api/user.address/setDefault', {
					id: address_id,
				}).then(res => {
					uni.hideLoading();
					uni.showToast({
						title: res.msg,
						duration: 2000
					});
					_this.getAddressList();
					// 下单页面参数confirm、订单修改地址参数order
					var type = _this.type;
					if (type == 'confirm') {
						uni.navigateBack()
						return;
					}
				}).catch(res => {
					console.log(res);
				})
			},

			// 编辑地址
			editClick(address_id) {
				console.log(address_id)
				uni.navigateTo({
					url: '../addressEdit/addressEdit?address_id=' + address_id
				})
			},

			// 删除地址
			delClick(address_id) {
				var _this = this;
				uni.showModal({
					title: '提示',
					content: '确定删除此地址吗？',
					
					success: function(res) {
						if (res.confirm) {
							// console.log('用户点击确定');
							_this.$u.post('/api/user.address/deleteAll', {
								id: address_id,
							}).then(res => {
								uni.hideLoading();
								console.log("==删除地址==");
								console.log(res);

								uni.showToast({
									title: res.msg,
									duration: 2000
								});
								_this.getAddressList();
							}).catch(res => {
								console.log(res);
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},

			// 页面跳转
			goPage(url) {
				uni.navigateTo({
					url: url
				})
			},
		}
	}
</script>

<style scoped>
	.addressList_box {
		padding: 0 30rpx;
		margin-bottom: 150rpx;
		overflow: hidden;
	}

	.addressItem {
		padding: 30rpx 20rpx;
		background-color: #fff;
		border-radius: 10rpx;
		overflow: hidden;
		margin-top: 20rpx;
	}

	.consigneeCon {
		/* padding: 20rpx 0; */
		font-size: 28rpx;
		color: #000;
		overflow: hidden;
		display: flex;
		align-items: center;
		margin-bottom: 20rpx;
	}
	
	.consigneeName text{
		margin-left: 30rpx;
	}
	
	.consigneeTel {
		margin-left: 30rpx;
	}

	.addressDetail {
		font-size: 30rpx;
		color: #666;
		overflow: hidden;
		padding-bottom: 30rpx;
	}

	.addressPost {
		padding-top: 30rpx;
		border-top: 1rpx solid #eee;
		overflow: hidden;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.addressCheck {
		display: flex;
		align-items: center;
	}

	.addressCheck text {
		margin-left: 10rpx;
	}
	
	.addressHandle{
		flex: 2;
		display: flex;
		justify-content: flex-end;
		align-items: center;
	}

	.addressDel {
		width: 40rpx;
		height: 40rpx;
		float: right;
	}

	.addressDel image {
		width: 100%;
		height: 100%;
	}

	/* 新增 */
	.formPost_con {
		width: 100%;
		position: fixed;
		left: 0;
		border-radius: 0;
		bottom: 0;
	}
	.formPost_con button{
		border-radius: 0;
	}
	.submitBtn{
		border-radius: 0;
	}
</style>
